﻿using NodeCanvas.Framework;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Core;

namespace Yoozoo.Gameplay.Liberty
{
    /// <summary>
    /// 负责蓝图的管理、可视化
    /// </summary>
    public class BlueprintManager : SingletonMono<BlueprintManager>
    {
        public bool showAreas = false;

#if UNITY_EDITOR
        [SerializeField]
        [DisplayOnly]
        private List<GraphOwner> enableGraphOwners;

        [SerializeField]
        [DisplayOnly]
        private List<GraphOwner> disableGraphOwners;

        [SerializeField]
        [DisplayOnly]
        private List<GraphOwner> pauseGraphOwners;

        [SerializeField]
        [DisplayOnly]
        private List<BlueprintColliderTrigger> colliderTriggers;

        private void Awake()
        {
            enableGraphOwners = new List<GraphOwner>(20);
            disableGraphOwners = new List<GraphOwner>(20);
            pauseGraphOwners = new List<GraphOwner>(20);
            colliderTriggers = new List<BlueprintColliderTrigger>(100);
            
            GraphOwner.onOwnerBehaviourStateChange += owner =>
            {
                RemoveGraphOwner(owner);
                if (owner.isRunning)
                {
                    enableGraphOwners.Add(owner);
                }
                else if (owner.isPaused)
                {
                    pauseGraphOwners.Add(owner);
                }
                else
                {
                    disableGraphOwners.Add(owner);
                }
            };

            BlueprintColliderTrigger.onStateChanged += trigger =>
            {
                // 暂时只有添加
                colliderTriggers.Add(trigger);
                trigger.ChangeAreaVisible(showAreas);
            };
        }

        private void Update()
        {
            // DEBUG........
            // if (Input.GetKeyDown(KeyCode.Space))
            // {
            //     LuaCallFunctions.OnRpgLevelComplete(123456);
            // }
        }

        private void RemoveGraphOwner(GraphOwner owner)
        {
            enableGraphOwners.Remove(owner);
            disableGraphOwners.Remove(owner);
            pauseGraphOwners.Remove(owner);
        }

        private void OnValidate()
        {
            foreach (var trigger in colliderTriggers)
            {
                trigger.ChangeAreaVisible(showAreas);
            }
            
        }
#endif
        
    }
}
